<?php
	$keyword = $_GET["txtSearchIndex"];
	require_once 'class/SanPhamDAO.php';
	require_once 'class/SanPhamDTO.php';
	require_once('class/GianHangDAO.php');
	require_once('class/LoaiSanPhamDAO.php');
	require_once('class/LikesDAO.php');
	$response = "";
	// lấy dữ liệu ra rồi phân trang:
	$ketqua_timkiem = array();
	$tinhtrang = 0;
	$start = 0;
	$display = 30;
	$ketqua_timkiem = SanPhamDAO::TimKiemSanPham_All($keyword, $tinhtrang, $start, $display);

	if(count($ketqua_timkiem) <= 0)
	{
		$response = $response."
		<div class='list-shop'>
			<ul class='search'>
				<li>
					<h3 align='center'>Tìm thấy ".count($ketqua_timkiem)." kết quả thỏa yêu cầu. </h3>
				</li>
			</ul>
		</div>
		";
	}
	else
	{
		$response = $response."
		<div class='list-shop'>
			<ul class='search'>
				<li>
					<h3 align='center'>Tìm thấy ".count($ketqua_timkiem)." kết quả thỏa yêu cầu. </h3>
				</li>
		";
		// xac dinh bao nhieu dong:
		$display = 5;
		// tinh tong so trang can hien thi:
		if(isset($_GET['page']) && (int)$_GET['page'])
		{
			$page = $_GET['page'];
		}
		else // neu chua xac dinh thi tinh so trang can hien thi:
		{						
			$record = count($ketqua_timkiem);
			if($record > $display)
			{
				$page = ceil($record/$display);
			}
			else
			{
				$page = 1;
			}
		}
		if(isset($_GET['start']) && (int)$_GET['start'])
		{
			$start = $_GET['start'];
		}
		else
		{
			$start = 0;
		}
		$ketqua_timkiem = SanPhamDAO::TimKiemSanPham_All($keyword, $tinhtrang, $start, $display);
		for($i = 0; $i<count($ketqua_timkiem);$i++)
		{
			$stt = $i + 1;
			$Ma = $ketqua_timkiem[$i]->Ma;
			$MaSanPham = $ketqua_timkiem[$i]->MaSanPham;
			$TenSanPham = $ketqua_timkiem[$i]->TenSanPham;
			$SoLuong = $ketqua_timkiem[$i]->SoLuong;
			$DonGiaGoc = $ketqua_timkiem[$i]->DonGiaGoc;
			$DonGiaBan = $ketqua_timkiem[$i]->DonGiaBan;
			$DacDiemSP = $ketqua_timkiem[$i]->DacDiemSP;
			$NgayDang = $ketqua_timkiem[$i]->NgayDang;
			$NgayCapNhat = $ketqua_timkiem[$i]->NgayCapNhat;
			$NguoiCapNhat = $ketqua_timkiem[$i]->NguoiCapNhat;
			$DaXoa = $ketqua_timkiem[$i]->DaXoa;
			$NguoiXoa = $ketqua_timkiem[$i]->NgayXoa;
			$NgayXoa = $ketqua_timkiem[$i]->NguoiXoa;
			$MaLoaiSP = $ketqua_timkiem[$i]->MaLoaiSP;
			$HinhAnh = $ketqua_timkiem[$i]->HinhAnh;
			$LuotXem = $ketqua_timkiem[$i]->LuotXem;
			$NgayDang = date("d/m/Y",strtotime($NgayDang));
			 
			$SoSaoTB = LikesDAO::TinhSoSaoTBCuaMotDoiTuong($Ma);
			$loaiSanPhamDto = LoaiSanPhamDAO::LayLoaiSanPhamTheoMaLoai($MaLoaiSP);
			$gianHangDto = GianHangDAO::LayGianHangTheoMa($loaiSanPhamDto->MaGianHang,0);
			$MaGianHang = $gianHangDto->MaGianHang;
			$TenGianHang = $gianHangDto->TenGianHang;
			$TenLoaiSP = $loaiSanPhamDto->TenLoaiSP;
			$response = $response."
				<li>
					<table width='100%' border='0' cellspacing='5' cellpadding='0' >
					  <tr valign='bottom'>
						<td width='11%' rowspan='2' class='text-color-normal-1'>
							<a href='chi_tiet_san_pham.php?maSanPham=$Ma&maGianHang=$MaGianHang'><img src='$HinhAnh' width='80' height='80' /></a>
						</td>
						<td class='text-color-bold-1' >
							<a href='chi_tiet_san_pham.php?maSanPham=$Ma&maGianHang=$MaGianHang'>
							$TenSanPham</a>
						</td>
						<td>
							Loại sản phẩm 
							<span class='text-color-normal-1'><a href='ds_san_pham.php?maLoaiSP=$MaLoaiSP&maGianHang=$MaGianHang'>
							$TenLoaiSP</a></span>
						</td>
						<td colspan='2' >
							Gian hàng
							<span class='text-color-normal-1' >
							<a href='gian_hang.php?maGianHang=$MaGianHang'>
							$TenGianHang</a></span>
						</td>
						<td width='7%'>#$stt</td>
					  </tr>
					  <tr class='text-normal-2'>
						<td width='21%'><span class='new-price'>$DonGiaBan VND</span></td>
						<td width='20%'>$LuotXem lượt xem</td>
						<td width='21%'>$SoSaoTB sao</td>
						<td width='20%'>Đăng ngày $NgayDang</td>
						<td>&nbsp;</td>
					  </tr>
					</table>
				</li>
				";
		}
		$response = $response."
			</ul>
		</div>
		";
		if($page > 1)
		{
			$response = $response."
			<div class='toolbox'>
				<div class='paging'>
					<div>
			";					
			$next = $start + $display;
			$prev = $start - $display;
			$current =  ($start/$display) + 1;
			// hien thi trang previous:	
			if($current != 1)
			{
				$response = $response."<a href='ket_qua_tim_kiem.php?txtSearchIndex=$keyword&start=$prev'> Trang trước </a>";
			}
			// hien thi so link:<a href='#' class='item current'>1</a> <a href='#' class='item'>2</a>
			for($i = 1; $i <= $page; $i++)
			{
				$response = $response."<a href='ket_qua_tim_kiem.php?txtSearchIndex=$keyword&start=".($display * ($i - 1))."' class='item'> $i </a>		
				";
				//echo "<a href='ds_san_pham.php?maGianHang=$magianhang&start=".($display * ($i - 1))."'> $i </a>";
			}
			// hien thi trang Next:
			if($current != $page)
			{
				$response = $response."<a href='ket_qua_tim_kiem.php?txtSearchIndex=$keyword&start=$next'> Trang kế </a>";
			}
			$response = $response."
					</div>
				</div>
			</div><!--end .paging -->";
		}
	}
	echo $response;
?>